<template>
  <div class="search-hot">
    <!-- 搜索 "提示" 组件{{keywords}} -->
    <h3>搜索“{{ keywords }}”</h3>
    <ul>
      <li
        v-for="(word, index) in suggest"
        @click.stop="$emit('search', word.keyword)"
        :key="index"
      >
        <div class="find"></div>
        <p>
          {{ word.keyword }}
        </p>
      </li>
    </ul>
  </div>
</template>

<script>
///search/suggest
//引入lodash库 使用其防抖函数
import _ from "lodash";

export default {
  props: {
    keywords: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      suggest: [],
    };
  },
  created() {
    this.getSuggest();
  },
  methods: {
    async getSuggest() {
      await this.axios
        .get(`/search/suggest?keywords=${this.keywords}&type=mobile`)
        .then((re) => {
          this.suggest = re.result.allMatch;
        });
    },
  },
  watch: {
    // keywords() {
    //   this.getSuggest();
    // },
    //侦听关键词的时候用防抖函数。节约网络资源
    keywords: _.debounce(function () {
      this.getSuggest();
    }, 200),
  },
};
</script>

<style lang="scss" scoped>
.search-hot {
  h3 {
    height: 50px;
    margin-left: 10px;
    padding-right: 10px;
    font-size: 15px;
    line-height: 50px;
    color: #507daf;
    margin-bottom: 10px;
    white-space: nowrap;
  }
  ul {
    margin-top: 5px;
    li {
      height: 45px;
      display: flex;
      align-items: center;
      padding: 5px 0;
      .find {
        width: 13px;
        height: 13px;
        background: url(@/assets/search.png) no-repeat;
        background-size: cover;
        margin: 0px 8px;
      }
      p {
        flex: 1;
      }
    }
  }
}
</style>